{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import subprocess\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Identify working directory\n",
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Make sure directory points to the highest level of the docker container, or the folder connected to \"Replication Attempt 1\"\n",
    "os.chdir(\"..\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create lists of cities and training data size to iterate over\n",
    "cities = [\"Santa Rosa\",\"Temecula\",\"South San Francisco\",\"Visalia\",\"Chula Vista\"]\n",
    "meetings = [\"One Meeting\",\"Two Meetings\",\"Three Meetings\",\"Four Meetings\",\"Five Meetings\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create Model Folder\n",
    "folder_path =Path(\"Step 3/Model Output/\")\n",
    "folder_path.mkdir(parents=True, exist_ok=True)  # Creates folder and parent directories if needed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# For each training data size (Meetings per year included) for each city, create model output folders\n",
    "for city in cities:\n",
    "    for meeting in meetings:\n",
    "        folder_path1 = Path(\"Step 3/Model Output/\"+city+\"/\"+meeting+ \"/Replication\")\n",
    "        folder_path1.mkdir(parents=True, exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# For each training data size (Meetings per year included) for each city, generate a training set and test set json file.\n",
    "for city in cities:\n",
    "    for meeting in meetings:\n",
    "        subprocess.run([\"python3\",Path(\"/opt/Step 3/Detectron Model/utils/cocosplit.py\"),\n",
    "                        \"--annotation-path\", Path(f\"/opt/Step 3/Annotated Training Set/{city}/{meeting}/result.json\"),\n",
    "                        \"--split-ratio\", str(.7),\n",
    "                        \"--train\", Path(f\"/opt/Step 3/Annotated Training Set/{city}/{meeting}/train.json\"),\n",
    "                        \"--test\", Path(f\"/opt/Step 3/Annotated Training Set/{city}/{meeting}/test.json\")])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# For each training data size (Meetings per year included) for each city, execute the fine-tuning of the object detection model\n",
    "for city in cities:\n",
    "    for meeting in meetings:\n",
    "        subprocess.run([\"python3\",Path(\"/opt/Step 3/Detectron Model/tools/train_net.py\"),\n",
    "                        \"--dataset_name\",\"prima-layout\",\n",
    "                        \"--json_annotation_train\",Path(f\"/opt/Step 3/Annotated Training Set/{city}/{meeting}/train.json\"),\n",
    "                        \"--image_path_train\",Path(f\"/opt/Step 3/Annotated Training Set/{city}/{meeting}\"),\n",
    "                        \"--json_annotation_val\",Path(f\"/opt/Step 3/Annotated Training Set/{city}/{meeting}/test.json\"),\n",
    "                        \"--image_path_val\",Path(f\"/opt/Step 3/Annotated Training Set/{city}/{meeting}\"),\n",
    "                        \"--config-file\",Path(\"/opt/Step 3/Detectron Model/configs/prima/fast_rcnn_R_50_FPN_3x.yaml\"),\n",
    "                        \"OUTPUT_DIR\", Path(f\"/opt/Step 3/Model Output/{city}/{meeting}/Replication\"),\n",
    "                        \"SOLVER.IMS_PER_BATCH\",str(1)])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
